﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workbench.Models;
using Workbench.Operations;
using Workbench.Persistance;
using Workbench.Shared;

namespace WorkbenchTests
{
    [TestClass]
    public class PerformanceTests
    {
        //[TestMethod]
        //public void PerformanceTests_joins_vs_stored_id()
        //{
        //    ProjectOperations.OpenProject(@"D:\Users\dimitar.NETAGESOLUTIONS\Documents\Nora.wbdb");
        //    var rootDirectory = ProjectOperations.GetRootDirectory();
        //    var random = new Random();
        //    var files = rootDirectory.GetFiles();
        //    var total = new TimeSpan();
        //    var posTypes = ProjectOperations.GetPOSTypes();
        //    Stopwatch sw = Stopwatch.StartNew();
        //    int wordsCount = 0;
        //    foreach (var file in files)
        //    {
        //        var words = LemmatizerOperations.GetWords(file);
        //        int processed = 0;
        //        int batch = 100000;
        //        while(true)
        //        {
        //            var step  = Math.Min(batch, words.Count - processed);
        //            var wordsToUpdate = words.Skip(processed).Take(step);
        //            LemmatizerOperations.AddPosTypeToWords(posTypes[random.Next(posTypes.Count)], wordsToUpdate);
        //            processed += step;
        //            if (processed == words.Count) break;

        //            Trace.WriteLine(string.Format("Updated {0} words for {1}ms", processed, sw.ElapsedMilliseconds));
        //        }
        //    }
        //    sw.Stop();
        //    Trace.WriteLine(string.Format("Updated {0} words for {1}ms", wordsCount, sw.ElapsedMilliseconds));

        //    long ms = 0;
        //    for (int i = 1; i <= 10000; i++)
        //    {
        //        files.ForEach(x => x.ContextIncluded = true);

        //        files[random.Next(rootDirectory.GetContextIncludedFiles().Count)].ContextIncluded = false;
        //        var res = ProjectOperations.GetPOSTypesWithStatistics(rootDirectory.GetContextIncludedFiles());
        //        total += TimeSpan.FromMilliseconds(ms);
        //        if (i % 50 == 0)
        //        {
        //            Debug.WriteLine(string.Format("i:{1} Avg :{0}ms", i, total.TotalMilliseconds / (double)i));
        //        }
        //    }
        //}

        //[TestMethod]
        //public void PerformanceTests_db_connection_overhead()
        //{
        //return;
        //    var db = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        //    var conBuilder = new SQLiteConnectionStringBuilder()
        //    {
        //        DataSource = db
        //    };


        //    var sw = Stopwatch.StartNew();
        //    Parallel.For(0, 10000, (i) =>
        //    {
        //        using (var con = new SQLiteConnection(conBuilder.ToString()))
        //        {
        //            con.Open();
        //            if (i % 100 == 0)
        //            {
        //                sw.Stop();
        //                Trace.WriteLine(string.Format("100 connections opened for: {0}ms", sw.ElapsedMilliseconds));
        //                sw.Restart();
        //            }
        //        }
        //    });
        //    sw.Stop();
        //    return;

        //    using (var con = new SQLiteConnection(conBuilder.ToString()))
        //    {
        //        con.Open();
        //        new SQLiteCommand("DELETE FROM Directories", con).ExecuteNonQuery();
        //    }

        //    using (var con = new SQLiteConnection(conBuilder.ToString()))
        //    {
        //        con.Open();
        //        for(int i = 1; i < 10000; i++)
        //        {
        //            int prev = i - 1;
        //            string parent = i == 1 ? "NULL" : prev.ToString();
        //            var name = string.Format("Directory {0}", i);
        //            var cmdText = string.Format("INSERT INTO Directories (name, parentdirectoryid) VALUES ('{0}', {1})", name, parent);
        //            new SQLiteCommand(cmdText, con).ExecuteNonQuery();
        //        }
        //    }
        //    sw.Stop();

        //    Trace.WriteLine(string.Format("Reusing connection: {0}ms", sw.ElapsedMilliseconds));


        //    sw.Start();
        //    for (int i = 1; i < 10000; i++)
        //    {
        //        var name = string.Format("Directory {0}", i);
        //        var prev = i - 1;
        //        string parent = i == 1 ? "NULL" : prev.ToString();
        //        var cmdText = string.Format("INSERT INTO Directories (name, parentdirectoryid) VALUES ('{0}', {1})", name, parent);
        //        using (var con = new SQLiteConnection(conBuilder.ToString()))
        //        {
        //            con.Open();
        //            new SQLiteCommand(cmdText, con).ExecuteNonQuery();
        //        }
        //    }
        //    sw.Stop();
        //    Trace.WriteLine(string.Format("Initializing: {0}ms", sw.ElapsedMilliseconds));
        ////}
        [TestMethod]
        public void PerformanceTests_parse_and_write_performance()
        {
            string text = File.ReadAllText("Alice In Wounderland.txt");
            text = string.Format("{0}\n\n{0}\n\n{0}", text);
            for (int i = 0; i < 10; i++)
            {
                var sw = Stopwatch.StartNew();
                var result = TextSegmentationService.Process(text, new Dictionary<string, TSUniqueWord>());
                sw.Stop();
                Trace.WriteLine(string.Format("Process: Split to {0} words of which {1} unique for {2}ms", result.Words.Count, result.DifferentForms, sw.ElapsedMilliseconds));
            }
        }
    }
}
